                          //
//  MeViewController.m
//  AdvertisementI
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 Apple inc. All rights reserved.
//

#import "MeViewController.h"
#import "Masonry.h"
@interface MeViewController ()

@end

@implementation MeViewController

- (void)viewDidLoad {
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [super viewDidLoad];
    //使背景为灰色
    self.view.backgroundColor = GrayColor;
    
    _bottomTableview= [[UITableView alloc] initWithFrame:CGRectMake(0, kScreenH*0.16, kScreenW, kScreenH ) style:UITableViewStyleGrouped];
    // _bottomTableview.backgroundColor = [UIColor greenColor];
    _bottomTableview.tag = 2;
    _bottomTableview.delegate = self;
    _bottomTableview.dataSource =self;
    _bottomTableview.scrollEnabled = NO;                              
    [self.view addSubview:_bottomTableview];
    
    [self PersonalInformationBt];
}
- (void)PersonalInformationBt{
    
    _PersonalInforButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 64,kScreenW, kScreenH*0.16)];
    _PersonalInforButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_PersonalInforButton];
    [_PersonalInforButton addTarget:self action:@selector(hitPersonButton:) forControlEvents:UIControlEventTouchDown];
    
    _HeadPortraitImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, kScreenH*0.16-20, kScreenH*0.16-20)];
    _HeadPortraitImageView.backgroundColor = [UIColor yellowColor];
    [_PersonalInforButton addSubview:_HeadPortraitImageView];
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.text = @"厦门旧影";
    // _nameLabel.backgroundColor = [UIColor orangeColor];
    [_PersonalInforButton addSubview:_nameLabel];
    
    
    _accountNumberLabel = [[UILabel alloc]init];
    _accountNumberLabel.textColor = [UIColor grayColor];
    _accountNumberLabel.font = [UIFont systemFontOfSize:14];
    
    _userdefults1 = [NSUserDefaults standardUserDefaults];
    NSString *acconutNumberString = [NSString stringWithFormat:@"%@",[_userdefults1 valueForKey:@"logname"]];
    _accountNumberLabel.text = [NSString stringWithFormat: @"用户名：%@",acconutNumberString ];
    [_PersonalInforButton addSubview:_accountNumberLabel];
    [self sureControlPosition];
}

//手动拉约束
- (void)sureControlPosition{
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(150,(kScreenH*0.16-20)*0.3));
        make.left.equalTo(_HeadPortraitImageView.mas_right).offset(20);
        make.bottom.equalTo(_PersonalInforButton.mas_top).offset((kScreenH*0.16)*0.5);
        
        
    }];
    [_accountNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200,(kScreenH*0.16-20)*0.3));
        make.left.equalTo(_HeadPortraitImageView.mas_right).offset(20);
        make.top.equalTo(_nameLabel.mas_bottom).offset(0);
    }];
    
    
    
}
//
-(void)hitPersonButton:(UIButton *)sender{
    AlterInformationViewController *alterInfoVC = [[AlterInformationViewController alloc]init];
    alterInfoVC.hidesBottomBarWhenPushed = YES;
    alterInfoVC.delegated = self; // 指定代理
//    将这个界面的值传输给下个界面
    alterInfoVC.headImageView.image = self.HeadPortraitImageView.image;
    [self.navigationController pushViewController:alterInfoVC animated:YES];
    NSLog(@"正在选取图片");
}
#pragma mark --实现代理方法---
- (void)passValue:(UIImage *)imageValue {
    
    self.HeadPortraitImageView.image = imageValue;
//    [self.bottomTableview reloadData];
}


//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1) {
        return 2;
    }
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor grayColor];
        switch (section) {
            case 0:
                cell.textLabel.text = @"我的收藏";
                break;
            case 1:
                if(row == 0){
                    cell.textLabel.text = @"我关注的人";
                }else{
                    cell.textLabel.text = @"关注我的人";
                }
                break;
            case 2:
                cell.textLabel.text = @"设置";
                break;
            case 3:
                cell.textLabel.text = @"退出登录";
                cell.backgroundColor = [UIColor redColor];
                break;
            default:
                break;
                
        }
    }
    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==3 && indexPath.row==0) {
        NSString *token = [APP_DEF objectForKey:TOKEN_KEY];
        AFHTTPRequestOperationManager *manager =[[AFHTTPRequestOperationManager alloc]init];
        manager.responseSerializer =[AFHTTPResponseSerializer serializer];
        
        NSDictionary *parameters = @{@"password":token};
        [manager POST:@"http://47.88.76.185:8080/Test/app_login" parameters:parameters
              success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
             //         NSString *rcode = [dic objectForKey:@"rcode"];
             NSString * rcode = [dic objectForKey:@"rcode"];
             int a = [rcode intValue];
             if (a)
             {
                 UIWindow * window = [UIApplication sharedApplication].keyWindow;
                 [window showHUDWithText:rcode Type:ShowPhotoNo Enabled:YES];
                 //                         [self.window showHUDWithText:[dic objectForKey:@"rcode"] Type:ShowPhotoNo Enabled:YES];
//                 _alertview.message = @"用户名或密码错误";
             }
             else
             {
                 UIWindow * window = [UIApplication sharedApplication].keyWindow;
                 NSString * str = @"退出成功";
                 [window showHUDWithText:str Type:ShowPhotoNo Enabled:YES];
                 //如果成功注销登录,删除存储的token和时间戳
                 [APP_DEF removeObjectForKey:TOKEN_KEY];
                 [APP_DEF removeObjectForKey:TIME_KEY];
                 [APP_DEF synchronize];
                loginViewController * vc =[[loginViewController alloc]init];
//                 window.rootViewController = vc;
                 [self.navigationController pushViewController:vc animated:YES];
             }
         } failure:nil];
    }
}
//调整 section之间的间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
    
}
//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    if (indexPath.section == 0) {
    //        if (indexPath.row == 0) {
    //            return kScreenH*0.16;
    //        }
    //
    //    }
    return kScreenH*0.08;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
